package com.zhou.bjgl.bjgl.business_web.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhou.bjgl.bjgl.business_web.entity.BClass;
import com.zhou.bjgl.bjgl.business_web.entity.BCollege;
import com.zhou.bjgl.bjgl.exception.BwException;
import com.zhou.bjgl.bjgl.exception.BwExceptionEnum;
import com.zhou.bjgl.bjgl.business_web.mapper.BClassMapper;
import com.zhou.bjgl.bjgl.business_web.mapper.BCollegeMapper;
import com.zhou.bjgl.bjgl.business_web.mapper.BTeacherMapper;
import com.zhou.bjgl.bjgl.req.b_class.BClassAddReq;
import com.zhou.bjgl.bjgl.req.b_class.BClassQueryReq;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhou.bjgl.bjgl.resp.BClassResp;
import com.zhou.bjgl.bjgl.resp.PageResp;
import com.zhou.bjgl.bjgl.business_web.service.BClassService;
import com.zhou.bjgl.bjgl.util.CopyUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;


/**
 * <p>
 * 班级servcie实现类
 * </p>
 *
 * @author jz
 * @since 2021-07-04
 */
@Service
public class BClassServiceImpl extends ServiceImpl<BClassMapper, BClass> implements BClassService {

    @Resource
    private BClassMapper bClassMapper;

    @Resource
    private BTeacherMapper bTeacherMapper;

    @Resource
    private BCollegeMapper bCollegeMapper;

    /**
     * 添加班级方法
     * @param req 班级请求实体
     */
    @Override
    public void add(BClassAddReq req) {
        BClass bClass = CopyUtil.copy(req, BClass.class);

        //查询学院是否存在
        QueryWrapper<BCollege> bCollegeQueryWrapper = new QueryWrapper<>();
        bCollegeQueryWrapper.eq("id", req.getCollegeId());
        BCollege bCollege = bCollegeMapper.selectOne(bCollegeQueryWrapper);
        if (bCollege == null) {
            throw new BwException(BwExceptionEnum.NO_COLLEGE);
        }

        if (ObjectUtils.nullSafeEquals(req.getTypeForEdit(), "add")) {
            //判断班级是否重名
            QueryWrapper<BClass> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("class_name",req.getClassName());
            BClass old = bClassMapper.selectOne(queryWrapper);
            if (old != null) {
                throw new BwException(BwExceptionEnum.NAME_EXISTED);
            }
            bClass.setDelStatus(0);
            int insert = bClassMapper.insert(bClass);
            if (insert == 0) {
                throw new BwException(BwExceptionEnum.INSERT_FAIL);
            }
        } else {
            //进行更新
            bClassMapper.updateById(bClass);
        }

    }

    /**
     * 查询班级是否存在
     * @param req
     * @return
     */
    @Override
    public PageResp<BClassResp> listClass(BClassQueryReq req) {
        //查询条件构造器
        QueryWrapper<BClass> bClassQueryWrapper = new QueryWrapper<>();

        if (req.getCollegeId() != null) {
            bClassQueryWrapper.eq("college_id",req.getCollegeId());
        }
        //进行分页
        PageHelper.startPage(req.getPage(),req.getSize());
        List<BClass> bClasses = bClassMapper.selectList(bClassQueryWrapper);

        //把查询结构拷贝到返回结果实体
        List<BClassResp> bClassResps = CopyUtil.copyList(bClasses, BClassResp.class);

        //构造返回结果，将实体信息和记录条数设置进入分页实体
        PageInfo<BClassResp> respPageInfo = new PageInfo<>(bClassResps);
        PageResp<BClassResp> pageResp = new PageResp<>();
        pageResp.setPageList(bClassResps);
        pageResp.setTotal(respPageInfo.getTotal());
        return pageResp;
    }


}
